#include "stdafx.h"
#include "SkyBox.h"

#include "ContentManager.h"

namespace Framework {

SkyBox::SkyBox(Game* game) : Mesh(game) {
    this->SetEffectFile("SkyBox.fx");
    this->SetMeshFile("sphere.x");
    this->SetCullMode(D3DCULL_CW);
}

SkyBox::~SkyBox(void) {
}

void SkyBox::Draw(FLOAT timeDelta) {
    D3DXVECTOR3 lightDir = D3DXVECTOR3(1, 1, 1);
    D3DXVec3Normalize(&lightDir, &lightDir);
    this->effect->SetValue("lightDir", &lightDir, sizeof(D3DXVECTOR3));

    D3DXMATRIX mWorldViewProjection = this->GetWorldMatrix() * this->GetGame()->GetCamera()->GetViewMatrix() * this->GetGame()->GetCamera()->GetProjectionMatrix();
    this->effect->SetMatrix("g_mWorldViewProjection", &mWorldViewProjection);
    this->effect->SetFloat("scale", 0.03f);
    this->effect->SetFloat("noiseScale", 4.1f);
    this->effect->SetFloat("noiseBias", -1.0f);
    this->effect->SetFloat("sunFallOff", 3.5f);
    this->effect->SetFloat("sunSharpness", 0.42f);
    this->effect->SetFloat("cloudSpeed", 0.052f);
    this->effect->SetFloat("noiseSpeed", 0.022f);
    this->effect->SetFloat("time_0_X", this->GetGame()->GetGameTime());
    this->effect->SetValue("cloudColor", &D3DXCOLOR(1, 1, 1, 1), sizeof(D3DXCOLOR));
    this->effect->SetValue("sunColor", &D3DXCOLOR(1, 0.8244f, 0.4399f, 1), sizeof(D3DXCOLOR));
    this->effect->SetValue("skyColor", &D3DXCOLOR(0, 0.4966f, 1, 1), sizeof(D3DXCOLOR));
    this->effect->SetTechnique("RenderSkyBox");

    __super::Draw(timeDelta);
}

void SkyBox::LoadContent() {
    __super::LoadContent();
    this->mesh->GetMaterials()->clear();
    this->mesh->GetMaterials()->push_back(d3d::WHITE_MTRL);
    IDirect3DVolumeTexture9* texture = this->GetGame()->GetContentManager()->LoadVolumeTexture("NoiseVolume.dds");
    this->mesh->GetTextures()->clear();
    this->mesh->GetTextures()->push_back(texture);

    this->effect->SetTexture("g_NoiseTexture", texture);
}

void SkyBox::Update(FLOAT timeDelta) {
    __super::Update(timeDelta);
    this->SetPosition(this->GetGame()->GetCamera()->GetPosition());
}

}
